import { handleRouter } from "./handle-router"

//上一个路由 
let prevRoute = ""
// 下一个路由
let nextRoute = window.location.pathname

export const getPrevRoute = () => prevRoute
export const getNextRoute = () => nextRoute

export const rewriteRouter = () => {
    // 它是监听addEventListener
    window.addEventListener('popstate', () => {
        handleRouter()
    })
    // 监视到 pushstate 变化
    const rawPushState = window.history.pushState
    // 重写覆盖一下 随后调用一下
    window.history.pushState = (...args) => {
        // 导航前
        prevRoute = window.location.pathname
        rawPushState.apply(window.history, args)//真正的改变历史纪录
        // 导航后
        nextRoute = window.location.pathname
        handleRouter()
    }
    // 监视到 replaceState 变化
    const rawReplaceState = window.history.replaceState
    window.history.replaceState = (...args) => {
        // 导航前
        prevRoute = window.location.pathname
        rawReplaceState.apply(window.history, args)
        // 导航后
        nextRoute = window.location.pathname
        handleRouter()
    }
}
// 第二部分 匹配子应用